<template>
    <div>
        <el-card class="box-card" shadow="never">
            <i class="el-icon-search"></i>
            <span>筛选搜索</span>
            <el-row :gutter="10" style="margin-top:15px;">
               <el-col :span='6'>
                  <el-input v-model="shop_name">
                    <template slot="prepend">店铺名称</template>
                  </el-input>
               </el-col>
               <el-col :span='6'>
                  <el-input v-model="shop_code">
                    <template slot="prepend">门店编码</template>
                  </el-input>
               </el-col>
               <el-col :span="6">
                    <el-select v-model="province" placeholder="所在省" clearable>
                        <el-option
                                v-for="item in allprovincelist"
                                :key="item.province"
                                :label="item.province"
                                :value="item.province">
                        </el-option>
                    </el-select>
              </el-col>

            </el-row>
            <el-row :gutter="20" class="yx_el_row">
                <el-col :span="4">
                    <el-button type="success" plain @click="search">筛选</el-button>
                    <!--<ExcelImport style="display:inline-block;margin-left:10px" :post_url="excel_post_url" :show_file_list="show_file_list" :accept_str="accept_str" :handle_result="handle_result"></ExcelImport>-->
                </el-col>
            </el-row>
            <div class="yx_line"></div>
            <div style="margin-top:10px;">
              <i class="el-icon-tickets"></i>
              <span>数据列表</span>
            </div>
            <el-table class="yx_table" border :data="shoplist">
                <el-table-column type="index" label="序号" width="80">
                </el-table-column>
                <el-table-column prop="shop_name" label="店铺名称" width="120">
                </el-table-column>
                <el-table-column prop="shop_code" label="门店编码" width="120">
                </el-table-column>
                <el-table-column prop="agent_name" label="经销商名称" width="120">
                </el-table-column>
                <el-table-column prop="shop_property" label="店铺性质" width="100">
                </el-table-column>
                <el-table-column prop="owner_name" label="店主姓名" width="100">
                </el-table-column>
                <el-table-column prop="tel_no" label="手机号" width="150">
                </el-table-column>
                <el-table-column prop="province" label="省" width="120">
                </el-table-column>
                <el-table-column prop="city" label="市" width="120">
                </el-table-column>
                <el-table-column prop="area" label="区" width="100">
                </el-table-column>
                <el-table-column prop="address" label="详细地址" width="180">
                </el-table-column>
                <el-table-column prop="register_time" label="注册时间">
                </el-table-column>
                <!-- <el-table-column label="操作">
                  <template slot-scope="scope">
                      <el-link type="success" @click="qrCode(scope.row.shop_code)">查看门店小程序码</el-link>
                  </template>
                </el-table-column> -->
            </el-table>
            <Pagination :total_count="total_count" :getList="getList"></Pagination>
        </el-card>
    </div>
</template>

<script>
    import { getShopList,getProvinceList,getQrCode } from '@/api/shop.js';
    import Pagination from '@/components/Pagination.vue';
    import ExcelImport from '@/components/ExcelImport.vue';
    export default {
        name: 'ShopList',
        components:{ Pagination,ExcelImport },
        inject:['reload'],
        data() {
            return {
                shoplist:[],
                total_count:0,
                shop_name:'',
                shop_code:'',
                province:'',
                allprovincelist:[],
                //excel
                excel_post_url:'/import-shop-list',
                show_file_list:false,
                accept_str:".csv",
            }
        },
        methods: {
            getList(current_page,page_size){
                this.$store.dispatch('loadCurrentPageSize',page_size);
                var send_data={
                    current_page:current_page,
                    page_size:page_size,
                    shop_name:this.shop_name,
                    shop_code:this.shop_code,
                    province:this.province
                }
                let that=this;
                getShopList(send_data).then(function(response){
                    if(response.data.code==1){
                      that.shoplist=response.data.list;
                      that.total_count=response.data.total_count;
                    }
                });
            },
            search(){
              //this.reload();
               this.getList(1,this.$store.getters.getCurrentPageSize);
            },
            add(){
                this.$router.push({
                    name:'shopEdit',
                    params:{id:-1,edit_type:1}
                });
            },
            edit(id,edit_type){
                this.$router.push({
                    name:'shopEdit',
                    params:{id:id,edit_type:edit_type}
                });
            },
            del(id){
                let that=this;
                this.$confirm('确认删除, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then((e) => {
                    var send_data={
                        id:id,
                        is_delete:1
                    };
                    delShop(send_data).then(function(response){
                        let type=response.data.code==1?'success':'error';
                        that.$message({
                            message:response.data.msg,
                            type:type,
                            onClose:function(e){
                                that.reload();
                            }
                        });
                    })
                })
            },
            //处理excel导入的结果
            handle_result(res){
               let that=this;
               let type=res.code==1?'success':'error';
               this.$message({
                   message:res.msg,
                   type:type,
                   onClose:function(e){
                      if(res.code==1){
                        that.reload();
                      }
                   }
               });
            },
            qrCode(shop_code){
               let data={
                  shop_code:shop_code
               };
               let that=this;
               const loading = this.$loading({
                         lock: true,
                         text: '正在生成小程序码...',
                         spinner: 'el-icon-loading',
                         background: 'rgba(0, 0, 0, 0.7)'
               });
               getQrCode(data).then(function(response){
                  loading.close();
                  if(response.data.code==1){
                      window.open(response.data.url);
                  }else{
                     that.$message({message:response.data.msg,type:'error'});
                  }
               })
            }
        },
        created() {
            let that=this;
            getProvinceList().then(function(response){
                if(response.data.code==1){
                    that.allprovincelist=response.data.list;
                }
            })
            that.getList(1,this.$store.state.default_page_size);
        },
    }
</script>
